﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GameSchoolApplication.Models;

namespace GameSchoolApplication.Repositories
{
    public class AssignmentRepository
    {
        gsDBDataContext link = new gsDBDataContext();

        /// <summary>
        /// Returns assignment with the specified ID
        /// Precondition: ID must exist
        /// Postcondition: Assignment has been returned
        /// Author: Aron Mar
        /// Date: 4.5.12
        /// </summary>
        /// <param name="ID">ID of assignment to get</param>
        public Assignment getAssignmentByID(int ID)
        {
            Assignment ass = (from u in link.Assignments
                               where u.assignmentID == ID
                               select u).First();
            return ass; 
        }

        /// <summary>
        /// Returns a queryable list of all assignments of the course with the specified ID
        /// Precondition: course must exist
        /// Postcondition: All assignments of the course have been returned
        /// Author: Aron Mar
        /// Date: 4.5.12
        /// </summary>
        /// <param name="courseID">The ID of the course</param>
        public IQueryable<Assignment> getAssignmentsOfCourse(int courseID)
        {
            IQueryable<Assignment> assigns = (from a in link.Assignments
                                              from cl in link.CourseLevels
                                              where cl.cID == courseID && a.lvlID == cl.lvlID
                                              select a);
            return assigns;
        }

        /// <summary>
        /// Returns a queryable list of all assignments of the level with the specified ID
        /// Precondition: level must exist
        /// Postcondition: All assignments of the level have been returned
        /// Author: Aron Mar
        /// Date: 7.5.12
        /// </summary>
        /// <param name="courseID">The ID of the course</param>
        public IQueryable<Assignment> getAssignmentsOfLevel(int levelID)
        {
            IQueryable<Assignment> assigns = (from a in link.Assignments
                                              where a.lvlID == levelID
                                              select a);
            return assigns;
        }

        /// <summary>
        /// Returns a list of all assignments the user has access to.
        /// </summary>
        /// <param name="userID"></param>
        public IQueryable<Assignment> getUserAssignments(string userID)
        {
            IQueryable<Assignment> ass = (from a in link.Assignments
                                          from cl in link.CourseLevels
                                          from e in link.StudentInClasses
                                          where e.stID == userID && e.cID == cl.cID && a.lvlID == cl.lvlID
                                          select a);
            return ass;
        }

        /// <summary>
        /// Returns a query-able list containing all assignments
        /// Precondition: 
        /// Postcondition: List has been returned
        /// Author: Aron Mar
        /// Date: 4.5.12
        /// </summary>
        public IQueryable<Assignment> getAllAssignments()
        {
            return link.Assignments;
        }

        /// <summary>
        /// Adds assignment to database
        /// Precondition: The user is a teacher
        /// Postcondition: Assignment has been added to database
        /// Author: Aron Mar
        /// Date: 4.5.12
        /// </summary>
        public bool addAssignment(Assignment ass)
        {
            try
            {
                link.Assignments.InsertOnSubmit(ass);
                save(); 
            }
            catch (Exception)
            {
                return false;
            }
            return true; 
        }

        /// <summary>
        /// Removes assignment
        /// Precondition: ID must exist
        /// Postcondition: Assignment has been removed
        /// Author: Aron Mar
        /// Date: 4.5.12
        /// </summary>
        /// <param name="assignmentID">ID of assignment to remove</param>
        public bool removeAssignment(int assignmentID)
        {
            Assignment ass = (from a in link.Assignments
                               where a.assignmentID == assignmentID
                               select a).First();
            if (ass != null)
            {
                try
                {                    
                    link.Assignments.DeleteOnSubmit(ass);
                }
                catch (Exception)
                {
                    return false;
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// Edits assignment
        /// Precondition: User must be an administrator/teacher and the assignment must exist
        /// Postcondition: Assignment information has been updated in the database
        /// Author: Aron Mar
        /// Date: 4.5.12
        /// </summary>
        /// <param name="assign">The edited assignment</param>
        public bool editAssignment(Assignment assign)
        {
            try
            {
                var ass = (from a in link.Assignments
                            where a.assignmentID == assign.assignmentID
                            select a).First();

                ass.assDescription = assign.assDescription;
                ass.CourseLevel = assign.CourseLevel;
                ass.deadLine = assign.deadLine;
                ass.name = assign.name;
                ass.reward = assign.reward;
                ass.lvlID = assign.lvlID;
                
                save();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

      

        /// <summary>
        /// Saves changes to database
        /// Precondition:
        /// Postcondition: Database has been updated
        /// Author: Aron og Bjarmi
        /// Date: 03.05.2012
        /// </summary>
        public void save()
        {
            link.SubmitChanges();
        }
    }
}